GBML manual / Lists

list_find

Gives back the place (the index; 0 is the first item) of the first item in target that equals value, or -1 if value is not in target at all. If you only want to know yes or no, list_contains does that.

Syntax

list_find(target, value) -> int

Arguments

NameType
targetlist
valueelement

Returns

int

Description

Gives back the place of the first item in target, a list, that equals value. Places are counted from 0, so the first item is place 0, the second is place 1, and so on.

If value is not in the list at all, you get -1. There is no place -1, so it can never be mistaken for a real answer: -1 always means "not there".

var at = list_find(scores, 20);   // 1, if scores is [10, 20, 30]
var no = list_find(scores, 99);   // -1, because 99 is not in the list

If you only want to know yes or no, and not where, list_contains answers that with true or false.

Always check for -1 before you use the answer

A place number is never below 0, so a simple test tells you whether the search found something:

/// @type {list<string>}
var keys = ["red", "blue", "green"];
var at = list_find(keys, "blue");
if (at >= 0) {
    list_remove(keys, at);    // blue is gone, and green moves down one place
}

Do not use the answer as a place before you have checked it. Reading keys[-1] is a mistake: the Game Boy Advance module, which the game is built on, checks every index and stops the game with an "Invalid index" message. A miss gives -1, so a search that finds nothing and goes straight to keys[at] ends the game.

Which place you get when there is more than one

The search starts at the front and stops at the first match. If value is in the list twice, you get the place of the one nearer the front:

/// @type {list<int>}
var scores = [5, 7, 5];
var at = list_find(scores, 5);   // 0, not 2

To find the next one, take the first one out with list_remove and search again, or loop over the list with [i].

What counts as equal

The same as list_contains, because they use the same search: numbers are equal when they are the same number, and text is equal when every letter is the same. So list_find(items, v) >= 0 is true exactly when list_contains(items, v) is true.

What happens in the awkward cases

You do thisWhat happens
search a list with no items-1
search for a value that is not there-1
search for a value that is there more than oncethe place of the first one
search a list that is fullworks as normal. The search only looks at the items the list holds
give it an arrayrefused when checked. An array has its own, array_find
use the answer as a place without checking itif it was -1, the game stops with "Invalid index"

It does not change the list

list_find only looks. The list is exactly the same afterwards.

In a script

A script that takes a list as a parameter works on the caller's own list (see Lists). So list_find inside a script searches the caller's list.

What it costs

Nothing is set aside. It looks at one item after another from the front until it finds a match, so a longer list takes longer, and a search that finds nothing looks at every item. No timing was measured.

Example

/// @type {list<int>}
var scores = [10, 20, 30];
var at20 = list_find(scores, 20);   // 1
var at99 = list_find(scores, 99);   // -1 - 99 is not in the list

On the GBA

A plain left-to-right search (find, the GBA module's own alias for std::find, the same call the contains function makes) that stops at the first match and counts how far it went - O(n) in how many items target holds, same cost as writing the loop by hand. It compares with ==, exactly like the contains function, so the answer is -1 when and only when the contains function says false. -1 is not a place, and target[-1] stops the game with "Invalid index" like any other bad index does - test the answer with >= 0 before using it as one.

See also: list_containsarray_findlist_removelist_insertlist_lengthListsData containers